﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Collections;

namespace PersonaLibrary.Controls
{
    [TemplatePart(Name = "PART_text", Type = typeof(TextBox))]
    public sealed class DropDownContentControl : Control
    {
        public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(DropDownContentControl));
        public static readonly DependencyProperty IsFilteredProperty = DependencyProperty.Register("IsFiltered", typeof(bool),
            typeof(DropDownContentControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(IsFilteredPropertyChanged)));
        public static readonly DependencyProperty FilteredTextProperty = DependencyProperty.Register("FilteredText", typeof(string), typeof(DropDownContentControl),
            new FrameworkPropertyMetadata(""));

        public event EventHandler IsFilteredChanged;

        private static void IsFilteredPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (((DropDownContentControl)sender).IsFilteredChanged != null) ((DropDownContentControl)sender).IsFilteredChanged(sender, new EventArgs());
        }

        public DropDownContentControl()
            : base()
        {
            this.DefaultStyleKey = typeof(DropDownContentControl);
        }
        
        public bool IsOpen
        {
            set
            {
                SetValue(IsOpenProperty, value);
            }
            get
            {
                return (bool)GetValue(IsOpenProperty);
            }
        }

        public bool IsFiltered
        {
            set
            {
                SetValue(IsFilteredProperty, value);
            }
            get
            {
                return (bool)GetValue(IsFilteredProperty);
            }
        }

        public string FilteredText
        {
            set
            {
                SetValue(FilteredTextProperty, value);
            }
            get
            {
                return (string)GetValue(FilteredTextProperty);
            }
        }

        public override void OnApplyTemplate()
        {
            if (GetTemplateChild("PART_text") != null)
            {
                ((TextBox)GetTemplateChild("PART_text")).TextChanged += new TextChangedEventHandler(DropDownContentControl_TextChanged);
                ((TextBox)GetTemplateChild("PART_text")).KeyDown += new System.Windows.Input.KeyEventHandler(DropDownContentControl_KeyDown);
            }

            base.OnApplyTemplate();
        }

        void DropDownContentControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Return && FilteredText != null && GetTemplateChild("filterButton") != null)
            {
                if (IsFiltered) IsFilteredPropertyChanged(this, new DependencyPropertyChangedEventArgs());
                ((System.Windows.Controls.Primitives.ToggleButton)GetTemplateChild("filterButton")).IsChecked = true;                
            }
        }

        void DropDownContentControl_TextChanged(object sender, TextChangedEventArgs e)
        {
            FilteredText = ((TextBox)sender).Text;
        }
    }
}
